/*
 * @lc app=leetcode.cn id=213 lang=javascript
 *
 * [213] 打家劫舍 II
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var rob = function (nums) {
  const N = nums.length
  if (N <= 3) return Math.max(...nums)
  function getMoney(nums) {
    const N = nums.length
    if (N <= 2) return Math.max(...nums)
    let i = 2
    const dp = [nums[0], Math.max(nums[0], nums[1])]
    while (i < N) dp[i] = Math.max(dp[i - 2] + nums[i], dp[i++ - 1])
    return dp[N - 1]
  }
  let robFirstHose = nums[0] + getMoney(nums.slice(2, N - 1))
  let letFirstHoseGo = getMoney(nums.slice(1))
  return Math.max(robFirstHose, letFirstHoseGo)
}
// const nums = [2, 3, 2]
// console.log(rob(nums))
// @lc code=end
